package springboot_demo.itheima.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import springboot_demo.itheima.mapper.UserMapper;
import springboot_demo.itheima.pojo.User;
import springboot_demo.itheima.service.UserService;
import springboot_demo.itheima.utils.Md5Util;
import springboot_demo.itheima.utils.ThreadLocalUtil;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 功能：服务实现类
 * 作者：Java课程项目组
 * 日期：2025年01月06日
 */
@Service//注册到ioc容器里面
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    //根据用户名查询用户,调用mapper执行sql
    @Override
    public User findByUsername(String username) {
        User u=userMapper.findByUserName(username);
        return u;
    }

    //注册逻辑
    @Override
    public void register(String username, String password) {
        //加密处理
        String md5String=Md5Util.getMD5String(password);
        //添加
        userMapper.add(username,md5String);
    }

    //更新用户基本信息逻辑
    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        //更新
        userMapper.update(user);
    }

    //更新用户头像逻辑
    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    //更新用户密码逻辑
    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }
}
